package com.qingke.besuper;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;

public class BeSuperApp {
	private static final String LIST="list";
	private static final String ASK="ask";
	private static final String ANSWER="answer";
	private static final String SCORE="score";
	private static final String HELP="help";
	private static final String EXIT="exit";
	
	private BeSuper beSuper=null;
	private User user=null;
	
	public void load(){
		if(!new File("beSuper.xml").exists()){
			beSuper=new BeSuper();
			BeSuperJaxb.marshal(beSuper);
		}else {
			beSuper=BeSuperJaxb.unmarshal();
		}
	}
	public void init(){
		
		load();
		
		String name=BeSuperConsole.prompt("Welcome to BeSuper,please enter your name:");
		for(User u:beSuper.getUsers()){
			if(name.equals(u.getName())){
				user=u;
				break;
			}
		}
		if(user==null){
			user=new User();
			user.setName(name);
			user.setScore(0);
			beSuper.getUsers().add(user);
		}
	}
	public void run() {
		
		init();
		BeSuperConsole.println("you can input help to look command");
		String command;
		while(true){
			command=BeSuperConsole.prompt("cmd>");
			if(LIST.equalsIgnoreCase(command)){
				listCommand();
			}
			else if(ASK.equalsIgnoreCase(command)){
				askCommand();
			}
			else if(ANSWER.equalsIgnoreCase(command)){
				answerCommand();
			}
			else if(SCORE.equalsIgnoreCase(command)){
				scoreCommand();
			}
			else if(HELP.equalsIgnoreCase(command)){
				helpCommand();
			}
			else if(EXIT.equalsIgnoreCase(command)){
				exitCommand();
				break;
			}
			else{
				errCommand();
			}
			
		}
		
		
	}
	
	public void listCommand(){
		Collections.sort(beSuper.getQuestions(),new Comparator<Question>(){
			public int compare(Question o1, Question o2) {
				if(o1.isSolved()!=o2.isSolved()){
					return o1.isSolved()?1:-1;
				}
				else{
					return (int) (o1.getId()-o2.getId());
				}
			}
		});
		BeSuperConsole.println("Questions");
		BeSuperConsole.println("================================");
		for(Question q:beSuper.getQuestions()){
			BeSuperConsole.println(q.toString());
		}
		BeSuperConsole.println("================================");
	}
	public void askCommand(){
		String str=BeSuperConsole.prompt("Please input question:");
		long id=System.currentTimeMillis();
		Question question = new Question();
		question.setId(id);
		question.setFrom(user.getName());
		question.setSolved(false);
		question.setContext(str);
		beSuper.getQuestions().add(question);
		BeSuperJaxb.marshal(beSuper);
		BeSuperConsole.println("ask is successful");
		}
	public void answerCommand(){
		String num=BeSuperConsole.prompt("Please question id:");
		if(!num.replaceAll("[0-9]","").equals("")){
			BeSuperConsole.println("err input");
			return ;
		}
		long id=Long.parseLong(num);
		Question question=null;
		for(Question q:beSuper.getQuestions()){
			if(q.getId()==id){
				question=q;
				break;
			}
		}
		if(question==null){
			BeSuperConsole.println("do not exist the id of the question");
		}else{
			BeSuperConsole.println(question.getContext());
			String str=BeSuperConsole.prompt("your answer:");
			Answer answer = new Answer();
			answer.setId(id);
			answer.setFrom(user.getName());
			answer.setContext(str);
			question.setSolved(true);
			user.setScore(user.getScore()+10);
			beSuper.getAnswers().add(answer);
			BeSuperJaxb.marshal(beSuper);
		}
	}
	public void scoreCommand(){
		BeSuperConsole.println("your score:"+user.getScore());
	}
	public void helpCommand(){
		BeSuperConsole.println("- list    list all questions");
		BeSuperConsole.println("- ask     ask a question");
		BeSuperConsole.println("- answer  select a question to answer");
		BeSuperConsole.println("- score   show your score");
		BeSuperConsole.println("- help    list all commands");
		BeSuperConsole.println("- exit    quit beSuper");
	}
	public void exitCommand(){
		BeSuperConsole.println("Bye bye");
	}
	public void errCommand(){
		BeSuperConsole.println("err input");
		BeSuperConsole.println("you can input help to look command");
	}

}
